<?php

namespace XWAM\Addon\PXCallSystem\Controller;

/**
 * BehindOperateController 控制器
 * @since 1.0 <2015-6-4> SoChishun <14507247@qq.com> Added.
 */
class BehindOperateController extends AppbaseController {

    /**
     * 切换到Crm
     * @since 1.0 <2015-7-17> SoChishun Added.
     */
    public function go_crm() {
        $this->redirect_to_crm();
    }

    public function get_nav_menus() {
        $arr = get_licence_data();
        if ($arr['username']) {
            $menuset = S('menuset_cti_' . $this->loginData['id']);
            if (!$menuset) {
                $menuset = $this->load_all_menus(0);
                S('menuset_cti_' . $this->loginData['id'], $menuset, array('expire' => 30));
            }
            $msg = $menuset ? array('status' => true, 'info' => $menuset) : array('status' => false, '没有菜单数据');
        } else {
            $msg = array('status' => false, 'info' => '未授权');
        }
        $this->ajaxReturn($msg);
    }

    /**
     * 加载菜单
     * @param int $pid
     * @return mixed
     * @since 1.0 2014-6-19 by sutroon
     */
    function load_all_menus($pid = 0) {
        $list = M('tuser_permission')->where("parentID=$pid and isMenu=1" . ($this->permissionRule ? ' and id in (' . $this->permissionRule . ')' : ''))->field('id, name, code, linkUrl as url')->select();
        if ($list) {
            foreach ($list as &$row) {
                $row['children'] = $this->load_all_menus($row['id']);
            }
        }
        return $list;
    }

    /**
     * 设置主题
     * @since 1.0 2014-7-12 by sutroon
     */
    public function set_theme($val = '') {
        $val = I('val');
        $err = '';
        if (!$val)
            $err .= '主题名称无效!<br />';
        if (!$this->loginData)
            $err .= '账户未登录!<br />';
        if ($err) {
            $this->error($err);
        }
        M('tuser_appsetting')->where('userID=\'' . $this->loginData['id'] . '\'')->setField('theme', $val);
        $this->update_loginData(array('theme' => $val));
        $this->success('主题设置成功!');
    }

    /**
     * 设置语言
     * @since 1.0 2014-7-15 by sutroon
     */
    public function set_language($val = '') {
        $err = '';
        if (!$val)
            $err .= '语言无效!<br />';
        if (!$this->loginData)
            $err .= '账户未登录!<br />';
        if ($err) {
            $this->error($err);
        }
        M('tuser_appsetting')->where('id=\'' . $this->loginData['id'] . '\'')->setField('language', $val);
        $this->update_loginData(array('language' => $val));
        $this->success('语言设置成功!');
    }

}
